package com.itheima.service.Impl;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckItemService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CheckItemService checkItemService;
   @Override
    public boolean addSetmeal(Setmeal setmeal) {
       return setmealMapper.add(setmeal);
   }
   @Override
    public List<String> findAll() {
       return setmealMapper.findAll();
   }

    @Override
    public PageResult findPageByCode(Pageable pageable, String code) {
        int pageNum = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int offset = (pageNum - 1) * pageSize;

        List<CheckItem> rows = setmealMapper.findByCode(code, offset, pageSize);
        long total = setmealMapper.countByCode(code);

        return new PageResult(total, rows);
    }

    @Override
    public PageResult findPageByName(Pageable pageable, String name) {
        int pageNum = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int offset = (pageNum - 1) * pageSize;

        List<CheckItem> rows = setmealMapper.findByName(name, offset, pageSize);
        long total = setmealMapper.countByName(name);

        return new PageResult(total, rows);
    }
    @Override
    public PageResult findPageDefault(Pageable pageable) {
       int pageNum = pageable.getPageNumber();
       int pageSize = pageable.getPageSize();
       int offset = (pageNum - 1) * pageSize;
       List<CheckItem> rows = setmealMapper.findPage(offset, pageSize);
       long total = setmealMapper.count();
       return new PageResult(total, rows);
    }
    @Override
    public Setmeal findById(Integer id) {
       return setmealMapper.findById(id);
    }
    @Override
    public boolean updateSetmeal(Setmeal setmeal) {
       return setmealMapper.update(setmeal);
    }
}
